﻿<%@ WebHandler Language="C#" Class="viewPurchaseHistory" %>

using System;
using System.Web;
using System.Collections.Generic;
using OneshoppingWeb;
using OneShoppingModel;
using OneShoppingBLL;
using System.Linq;
using OneShoppingUtility;
using System.Web.SessionState;

public class viewPurchaseHistory : IHttpHandler, IRequiresSessionState
{

    public void ProcessRequest(HttpContext context)
    {
        try
        {     
          SystemUser user = (SystemUser)context.Session["user"];
           
          //  int pageIndex = Convert.ToInt32(context.Request["pageIndex"]);
          //  int pageIndex = 1;
            Dictionary<int, List<viewHistory>> d = new Dictionary<int, List<viewHistory>>();
            
            
            List<Activitys> lacs = new List<Activitys>();
            List<viewHistory> list = viewHistoryBLL.GetInstance().GetList(r => r.UserId == user.UserId);
            foreach (viewHistory h in list)//取出listId
            {
                try
                {
                    d.Add(h.ListId, null);
                }
                catch { 
                
                }
            }

            var dicSort = from dd in d

                          orderby dd.Key

                          descending

                          select dd;


            foreach (var item in dicSort)
            {
                Activitys acs = new Activitys();
                acs.listId = item.Key;
                 List<viewHistory> vh=new List<viewHistory>();
                vh=viewHistoryBLL.GetInstance().GetList(r => r.UserId == user.UserId && r.ListId==item.Key);
                String str = "";
                foreach (var item1 in vh)
                {
                    if (item1.BuyStatus == 0) {
                        str = "未开奖";
                        break;
                    }
                    if (item1.BuyStatus == 3) {
                        str = "活动失败";
                        break;
                    }
                }
                foreach (var item1 in vh)
                {
                    if (item1.BuyStatus == 1)
                    {
                        str = "中奖";
                        break;
                    }
                    if (item1.BuyStatus == 2)
                    {
                        str = "未中奖";
                    }
                }
                acs.goodsName =vh[0].GoodsName;
                acs.buyNum = vh.Count;
                acs.status = str;
                acs.aNum = ActivityBLL.GetInstance().GetEntity(r => r.ListId == acs.listId).Total;
                lacs.Add(acs);
            }
            context.Response.Write(JsonHelper.SerializeObject(lacs));
        }
        catch (Exception ee){
           context.Response.Write((int)Protocol_activity.GET_EXCEPTION);//获取异常
            // context.Response.Write(ee.ToString());
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

    private class Activitys
    {
        public int listId { set; get; }
        public string goodsName { set; get; }
        public int buyNum { set; get; }
        public int aNum { set; get; }
        public String status { set; get; }
    }

}